import httpx
import json
import time
import execjs


headers = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36"
}

url = "https://u6.y.qq.com/cgi-bin/musics.fcg"

params = {
    "_": int(time.time()*1000),
    "sign": "zzb52316b80jl16yycj9x3yqktq2fevbw7a098d84"
}
data = {
    "comm": {
        "cv": 4747474,
        "ct": 24,
        "format": "json",
        "inCharset": "utf-8",
        "outCharset": "utf-8",
        "notice": 0,
        "platform": "yqq.json",
        "needNewCode": 1,
        "uin": 0,
        "g_tk_new_20200303": 5381,
        "g_tk": 5381
    },
    "req_1": {
        "module": "musicToplist.ToplistInfoServer",
        "method": "GetDetail",
        "param": {
            "topid": 4,
            "offset": 0,
            "num": 20,
            "period": ""
        }
    }
}

client = httpx.Client(http2=True)
data = json.dumps(data, separators=(',', ':'))

# python调用JS代码生成加密参数sign
with open('request_params_decrypt.js', 'r', encoding='utf-8') as f:
    js_code = f.read()
    ctx = execjs.compile(js_code)
    sign = ctx.call("getSign", data)
    print(sign)


params['sign'] = sign
response = client.post(url, headers=headers, params=params, data=data)
print(response.json())
